using System;
using System.Collections.Generic;
using System.Text;
using GarageGames.Torque.Materials;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GarageGames.Torque.XNA;
using GarageGames.Torque.Core;
using Xyne.Objects.Common;
using System.Collections;
using Xyne.Objects;
using GarageGames.Torque.GFX;

namespace Xyne.Utilities
{   
    public class MaterialHelper
    {
        #region Constants
        private const string CardIconPath = @"data\images\ui\card\icons\";
        #endregion

        #region Variables
        private static MaterialHelper _instance;
        private Hashtable _textures;
        private static object _lockObject;
        #endregion

        #region Properties
        public static MaterialHelper Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new MaterialHelper();
                }
                return _instance;
            }
        }

        public static object LockObject
        {
            get
            {
                if (_lockObject == null)
                {
                    _lockObject = new object();                     
                }
                return _lockObject;
            }
        }
        #endregion

        #region Constructor
        public MaterialHelper()
        {
            _textures = new Hashtable();
            _lockObject = new object();
        }
        #endregion

        public static Texture2D CombineTextures(Texture2D destTexture, Texture2D sourceTexture, Vector2 destSize, Vector2 position, Vector2 sourceSize)
        {
            if (!GFXDevice.IsWideScreen((int)Game.Instance.ClientBounds.X, (int)Game.Instance.ClientBounds.Y))
            {
                position = position * .7f;
                sourceSize = sourceSize * .7f;
            }
            RenderTarget2D myTarget = new RenderTarget2D(Game.Instance.GraphicsDevice, ((int)destSize.X), ((int)destSize.Y), 1, destTexture.Format);

                TorqueEngineComponent.Instance.Game.GraphicsDevice.SetRenderTarget(0, myTarget);
                TorqueEngineComponent.Instance.Game.GraphicsDevice.Clear(ClearOptions.Target, new Vector4(0f, 0f, 0f, 0f), 0.0f, 0);

                // Blit to the other texture, or so I thought :(
                SpriteBatch spriteBatch = new SpriteBatch(TorqueEngineComponent.Instance.Game.GraphicsDevice);

                spriteBatch.Begin();
                spriteBatch.Draw(destTexture, new Rectangle(0, 0, ((int)destSize.X), ((int)destSize.Y)), Color.White);
                spriteBatch.Draw(sourceTexture, new Rectangle(((int)position.X), ((int)position.Y), ((int)sourceSize.X), ((int)sourceSize.Y)), Color.White);
                spriteBatch.End();

                TorqueEngineComponent.Instance.Game.GraphicsDevice.SetRenderTarget(0, null);
            
            return myTarget.GetTexture();
        }

        public static Texture2D DrawTextOnTexture(Texture2D destTexture, XyneText xyneText, Vector2 destSize)
        {
            RenderTarget2D myTarget = new RenderTarget2D(Game.Instance.GraphicsDevice, ((int)destSize.X), ((int)destSize.Y), 1, destTexture.Format);

                TorqueEngineComponent.Instance.Game.GraphicsDevice.SetRenderTarget(0, myTarget);
                TorqueEngineComponent.Instance.Game.GraphicsDevice.Clear(ClearOptions.Target, new Vector4(0f, 0f, 0f, 0f), 0.0f, 0);

                // Blit to the other texture, or so I thought :(
                SpriteBatch spriteBatch = new SpriteBatch(TorqueEngineComponent.Instance.Game.GraphicsDevice);

                spriteBatch.Begin();
                spriteBatch.Draw(destTexture, new Rectangle(0, 0, ((int)destSize.X), ((int)destSize.Y)), Color.White);
                spriteBatch.DrawString(xyneText.Font.Instance, xyneText.Text, xyneText.Position, xyneText.Color);
                spriteBatch.End();

                TorqueEngineComponent.Instance.Game.GraphicsDevice.SetRenderTarget(0, null);
            
            return myTarget.GetTexture();
        }

        public Texture2D GetButtonTexture(ButtonEnum button)
        {
            if (!_textures.ContainsKey(button))
            {
                switch (button)
                {
                    case ButtonEnum.A:
                        _textures.Add(button, Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\controller\xboxControllerButtonA"));
                        break;
                    case ButtonEnum.B:
                        _textures.Add(button, Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\controller\xboxControllerButtonB"));
                        break;
                    case ButtonEnum.X:
                        _textures.Add(button, Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\controller\xboxControllerButtonX"));
                        break;
                    case ButtonEnum.Y:
                        _textures.Add(button, Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\controller\xboxControllerButtonY"));
                        break;
                }
            }
            return (Texture2D)_textures[button];
        }

        

        public SimpleMaterial GetIconMaterial(string iconName)
        {
            if (iconName == null)
            {
                iconName = "transparenticon";
            }
            SimpleMaterial materialToReturn = null;
            
            materialToReturn = new SimpleMaterial();
            materialToReturn.Name = "Icon." + iconName+".png";
            materialToReturn.IsColorBlended = true;
            materialToReturn.TextureFilename = CardIconPath + iconName;

            return materialToReturn;
            
        } 

        public Texture2D GetIconTexture(string iconName, int iconIndex)
        {
            if (iconName == null)
            {
                iconName = "transparenticon";
            }
            else
            {
                string[] nameSplit = iconName.Split('\\');
                if (nameSplit.Length > 1)
                {
                    iconName = iconName + @"\" + nameSplit[1] + "icon" + iconIndex.ToString();
                }
                else
                {

                    iconName = "transparenticon";
                }
            }


            return Game.Instance.Content.Load<Texture2D>(@".\"+CardIconPath + iconName);
        }
    }
}
